package com.example.teamwork.impl;
import com.example.teamwork.Shrio.Exception.ServiceException;
import com.example.teamwork.converter.VisitorConverter;
import com.example.teamwork.informationGet.PageVO;
import com.example.teamwork.informationGet.VisitorVO;
import com.example.teamwork.mapper.InfocountMapper;
import com.example.teamwork.mapper.InterMapper;
import com.example.teamwork.mapper.OrderMapper;
import com.example.teamwork.mapper.VisitorMapper;
import com.example.teamwork.pojo.Infocount;
import com.example.teamwork.pojo.Inter;
import com.example.teamwork.pojo.Order;
import com.example.teamwork.pojo.Visitor;
import com.example.teamwork.service.VisitorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 〈访客管理〉<br>
 * 〈〉
 * @author  SongPeng
 * @date  2021/11/09 14:25
 * @version 1.0
 */

@Service
public class VisitorServiceImpl implements VisitorService {
	@Autowired
	private VisitorMapper visitorMapper;
	@Autowired
	private InterMapper interMapper;
	@Autowired
	private VisitorConverter visitorConverter;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private InfocountMapper infocountMapper;
	@Transactional
	@Override
	public void add(VisitorVO visitorVO) {
		@NotBlank(message = "访客名不能为空") String visitorName = visitorVO.getVisitorName();
	   @NotNull(message = "被访人id不能为空") Long InterId = visitorVO.getInterId();
	  Inter inter= interMapper.selectByPrimaryKey(InterId);
		if(inter==null){
			throw new ServiceException("请确定要访问的人员信息是否存在");
	   }
	   Visitor visitor=new Visitor();
		BeanUtils.copyProperties(visitorVO,visitor);
		visitor.setEtime(new Date());
		visitorMapper.insert(visitor);
	}
	/**
	 * 当前访客列表
	 *
	 * */
	@Override
	public PageVO<VisitorVO> findVisitorList(Integer pageNum, Integer pageSize, VisitorVO visitorVO,Date startTime,Date endTime) {
		PageHelper.startPage(pageNum,pageSize);
		Example o = new Example(Visitor.class);
		String visitorName = visitorVO.getVisitorName();
		Integer sex = visitorVO.getVisitorSex();
		String nation=visitorVO.getVisitorNation();
		String Address=visitorVO.getVisitorAddress();
		String visitorPhone=visitorVO.getVisitorPhone();
		Example.Criteria criteria = o.createCriteria();
		//降序排序
		o.setOrderByClause("etime desc");
		if(visitorName!=null&&!"".equals(visitorName)){
			criteria.andLike("visitorName","%"+visitorName+"%");
		}
		if(sex!=null){
			criteria.andEqualTo("visitorSex",sex);
		}
		if(nation!=null&&!"".equals(nation)){
			criteria.andEqualTo("visitorNation",nation);
		}
		if(Address!=null&&!"".equals(Address)){
			criteria.andEqualTo("visitorAddress","%"+Address+"%");
		}
		if(visitorPhone!=null){
			criteria.andEqualTo("visitorPhone",visitorPhone);
		}
		criteria.andBetween("etime",startTime,endTime);
		List<Visitor> visitorList = visitorMapper.selectByExample(o);
		PageInfo<Visitor> info=new PageInfo<>(visitorList);
		List<VisitorVO> visitorVOS = visitorConverter.converterToVisitorVOList(visitorList);
		return new PageVO<>(info.getTotal(),visitorVOS);
	}

	@Override
	public List<Map<String, Object>> VisitorReport() {
		return visitorMapper.VisitorReport();
	}
	@Override
	public void set(Long visitorId) {
		Visitor visitor=visitorMapper.selectByPrimaryKey(visitorId);
		visitor.setLtime(new Date());
		visitorMapper.updateByPrimaryKeySelective(visitor);
	}

	@Override
	public void delete(Long visitorId) {
		visitorMapper.deleteByPrimaryKey(visitorId);
	}
	/**
	 * 根据时间统计访客
	 * */

	@Override
	public ArrayList statistics(Date startTime, Date endTime) {
		Example enter=new Example(Visitor.class);
		Example leave=new Example(Visitor.class);
		Example ordercount=new Example(Order.class);
		Example orderPass=new Example(Order.class);
		Example orderdeny=new Example(Order.class);
		Example.Criteria criteria = enter.createCriteria();
		Example.Criteria criterialeave=leave.createCriteria();
		Example.Criteria criteriaorder = ordercount.createCriteria();
		Example.Criteria criteriaorderget=orderPass.createCriteria();
		Example.Criteria criteriaorderrefuse=orderdeny.createCriteria();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(startTime==null && endTime==null){
			try {
				startTime = dateFormat.parse("2021-01-00 00:00:00");
				endTime=new Date();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else if(endTime==null){
			endTime=new Date();
		}else if(startTime==null){
			try {
				startTime = dateFormat.parse("2021-01-00 00:00:00");
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		//时间段进入人数
		criteria.andBetween("etime",startTime,endTime);
		Integer enterNumber=visitorMapper.selectCountByExample(enter);
		//时间段离开人数
		criterialeave.andBetween("ltime",startTime,endTime);
		Integer leaveNumber=visitorMapper.selectCountByExample(leave);
		//总预约人数
		criteriaorder.andBetween("orderVisittime",startTime,endTime);
		Integer orderCount=orderMapper.selectCountByExample(ordercount);
		//预约通过人数
		criteriaorderget.andEqualTo("orderStatus",1);
		criteriaorderget.andBetween("orderVisittime",startTime,endTime);
		Integer orderpass=orderMapper.selectCountByExample(orderPass);
		//预约未通过人数
		criteriaorderrefuse.andEqualTo("orderStatus",0);
		criteriaorderrefuse.andBetween("orderVisittime",startTime,endTime);
		Integer orderrefuse=orderMapper.selectCountByExample(orderdeny);
		ArrayList arrayList=new ArrayList();
		//使用@ResponseBody，返回的map会被springmvc使用json lib 转换成json字符串
		//组合成 json字符串"{\"name\" : \"zhangsan\", \"age\" : 20}"
		Map map=new HashMap();
		map.put("name","进入人数");
		map.put("value",enterNumber);
		arrayList.add(map);
		Map map1=new HashMap();
		map1.put("name","离开人数");
		map1.put("value",leaveNumber);
		arrayList.add(map1);
		Map map2=new HashMap();
		map2.put("name","预约总人数");
		map2.put("value",orderCount);
		arrayList.add(map2);
		Map map3=new HashMap();
		map3.put("name","预约通过人数");
		map3.put("value",orderpass);
		arrayList.add(map3);
		Map map4=new HashMap();
		map4.put("name","预约拒绝人数");
		map4.put("value",orderrefuse);
		arrayList.add(map4);
		//将统计出来的数据添加到数据库
		Infocount infocount=new Infocount();
		infocount.setInternumber(enterNumber);
		infocount.setLeavenumber(leaveNumber);
		infocount.setOrdercount(orderCount);
		infocount.setOrderpassnumber(orderpass);
		infocount.setOrderrejectnumber(orderrefuse);
		infocount.setGetday(new Date());
		infocountMapper.insertSelective(infocount);
		return arrayList;
	}

	@Override
	public VisitorVO detail(Long visitorId) {
		Visitor visitor=visitorMapper.selectByPrimaryKey(visitorId);
		VisitorVO visitorVO=visitorConverter.converterToVisitorVO(visitor);
		System.out.println(visitorVO+"here");
		return visitorVO;
	}
}
